//
//  SimpleAudioPlayer.m
//  SimpleAudioTest
//
//  Created by Handymood on 11-5-27.
//  Copyright 2011 __Hanydmood__. All rights reserved.
//

#import "SimpleAudioPlayer.h"

@implementation SimpleAudioPlayer
@synthesize snd;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)initAudioPlayer:(NSString *)audioPath
              autoPlay:(BOOL) inAutoPlay 
                isLoop:(BOOL) inLoop
                isButtonShow:(BOOL) inButtonShow
{
    type = @"AUDIO_PLAYER";
    NSString *fileNamePrefix = [audioPath  substringWithRange:NSMakeRange(0, [audioPath  length] - 4)];
    NSString *fileNameSuffix = [audioPath  substringWithRange:NSMakeRange([audioPath length]-3,3)];
    
    NSString *path;
    NSURL *soundFile;
    
    path = [[NSBundle mainBundle] pathForResource:fileNamePrefix   ofType:fileNameSuffix];
    soundFile = [NSURL fileURLWithPath:path ];
    
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    [audioPlayer setDelegate:self];
    
    if(inLoop)
        audioPlayer.numberOfLoops = -1;
    else
        audioPlayer.numberOfLoops = 0;
    
    if(inAutoPlay)
        [audioPlayer play];
    
    if(inButtonShow)
    {
        playButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
        pauseButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
        
        [playButton addButtonFace:@"sound_play_btn" normalImgSuffix:@"png" downImgPrefix:@"sound_play_btn" downImgSuffix:@"png"];
        [playButton addTarget:self action:@selector(mouseDownProcPlay:) forControlEvents:UIControlEventTouchDown];
        
        [pauseButton addButtonFace:@"sound_mute_btn" normalImgSuffix:@"png" downImgPrefix:@"sound_mute_btn" downImgSuffix:@"png"];
        [pauseButton addTarget:self action:@selector(mouseDownProcPause:) forControlEvents:UIControlEventTouchDown];
        
        [self addSubview:playButton];
        [self addSubview:pauseButton];
        
        //set frame for interactive
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 
                                                   playButton.frame.size.width, playButton.frame.size.height);
        
        playButton.hidden = TRUE;
        pauseButton.hidden = TRUE;
        
        if(inAutoPlay)
        {
            playButton.hidden = FALSE;
        }
        else
        {
            pauseButton.hidden = FALSE;
        }
    }
    
    snd = [[NSString alloc] initWithString:audioPath];
}

-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    //ONLY WILL TRIGGER IN NON LOOP MODE
    if(playButton)
    {
        pauseButton.hidden = FALSE;
        playButton.hidden = YES;
    }
}

-(void) mouseDownProcPlay:(id)sender
{
    [audioPlayer pause];
    pauseButton.hidden = FALSE;
    playButton.hidden = TRUE;

}

-(void) mouseDownProcPause:(id)sender
{
    [audioPlayer play];
    pauseButton.hidden = YES;
    playButton.hidden = FALSE;
}


-(void) manuallyMusicOn
{
    if (audioPlayer != Nil) 
    {
        [audioPlayer play];
        pauseButton.hidden = YES;
        playButton.hidden = FALSE; 
    }
}

-(void) manuallyMusicOff
{
    if (audioPlayer != Nil) 
    {
        if (audioPlayer != Nil && [audioPlayer isPlaying]) 
        {
            [audioPlayer stop];
        }

        pauseButton.hidden = FALSE;
        playButton.hidden = TRUE; 
    }
}

-(void)setXY:(CGFloat) inx 
        offy:(CGFloat) iny
{
    self.frame = CGRectMake(inx, iny, self.frame.size.width, self.frame.size.height);
}

-(void) buttonSizeAdjust:(CGFloat) inwidth
                   height:(CGFloat) inheight
{
    if (pauseButton == Nil) 
    {
        return;
    }
    
    pauseButton.frame = CGRectMake(pauseButton.frame.origin.x,pauseButton.frame.origin.y,inwidth,inheight);
    playButton.frame = CGRectMake(playButton.frame.origin.x,playButton.frame.origin.y,inwidth,inheight);
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, inwidth, inheight);
}

-(void)pause
{
    NSLog(@"PAUSE");
}

-(BOOL) musicOn
{
    if (audioPlayer != Nil)
    {
        return audioPlayer.playing;
    }
    else
    {
        return FALSE;
    }
}

-(void)reset
{
    if(playButton)
    {
        [playButton removeTarget:self action:@selector(mouseDownProcPlay:) forControlEvents:UIControlEventTouchDown];
        [pauseButton removeTarget:self action:@selector(mouseDownProcPause:) forControlEvents:UIControlEventTouchDown];    
    
        [playButton setUserInteractionEnabled:NO];
        [pauseButton setUserInteractionEnabled:NO];
    
        [playButton removeFromSuperview];
        [pauseButton removeFromSuperview];  
    }
    
    //[audioPlayer pause];
    [audioPlayer stop];
    audioPlayer.delegate = Nil;
    [audioPlayer release];
     
}

- (void)dealloc
{
    [super dealloc];
}

@end
